<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #box {
            width: 350px;
            height: 400px;
            margin-left: 100px;
            margin-top: 100px;
            position: relative;
        }

        #small_box {
            border: 1px solid #000;
            overflow: hidden;
            position: relative;
        }

        #mask {
            width: 100px;
            height: 100px;
            background-color: rgba(255, 255, 0, 0.4);
            position: absolute;
            left: 0;
            top: 0;
            cursor: move;
            display: none;
        }

        #big_box {
            width: 600px;
            height: 600px;
            border: 1px solid #000;
            overflow: hidden;
            position: absolute;
            left: 360px;
            top: 0;
            display: none;
        }

        #big_box img {
            position: absolute;
        }

        #box ul {
            list-style: none;
            width: 100%;
            margin-top: 10px;
        }

        #box ul li {
            float: left;
            margin-right: 10px;
        }

        .list_box{
            width: 600px;
            height: 600px;
            border: 1px solid #000;
            overflow: hidden;
            position: absolute;
            left: 360px;
            top: 0;
            display: none;
        }
    </style>
</head>

<body>
    <div id="box">
        <div id="small_box">
            <img src="images/pic001.jpg" alt="">
            <span id="mask"></span>
        </div>
        <div id="big_box">
            <img src="images/pic01.jpg" alt="">
        </div>
        <!--列表-->
        <ul id="list">
            <li><img src="images/pic0001.jpg" alt=""></li>
            <li><img src="images/pic0002.jpg" alt=""></li>
            <li><img src="images/pic0003.jpg" alt=""></li>
        </ul>
        <div class="list_box">
            <img src="./images/pic0001.jpg" alt="">
        </div>
    </div>
</body>
<script>


    // 获取 元素对象
    var infoImg = document.querySelector('#small_box');
    var mask = document.querySelector('#mask');
    var big = document.querySelector('#big_box');
    // var lists = document.querySelector('#list').querySelectorAll('li');
    // var img = document.querySelector('.list_box');

    // 鼠标移动到 infoImg 盒子中显示 遮罩和大图盒子
    infoImg.addEventListener('mouseover', function () {
        mask.style.display = 'block';
        big.style.display = 'block';
    })
    // 鼠标移开 infoImg 盒子中隐藏 遮罩和大图盒子
    infoImg.addEventListener('mouseout', function () {
        mask.style.display = 'none';
        big.style.display = 'none';
    })





    // lists.addEventListener('mouseover',function(){
    //     img.style.display = 'block';
    // })
    // lists.addEventListener('mouseout',function(){
    //     img.style.display = 'none';
    // })



    


    // 1. 给 infoImg 绑定移动事件
    infoImg.addEventListener('mousemove', function (e) {

        var maskX = e.pageX - mask.offsetWidth - mask.offsetWidth / 2;
        var maskY = e.pageY - mask.offsetWidth - mask.offsetHeight / 2;

        var x = maskX - infoImg.offsetLeft;
        var y = maskY - infoImg.offsetTop;
        // 这遮罩的最大可移动区域
        var maskMax = infoImg.offsetWidth - mask.offsetWidth;
        // 判断 设置的 位置 不能小于 0
        if (x <= 0) {
            x = 0;
        } else if (x >= maskMax) {
            x = maskMax;
        }
        // 判断设置最大区域
        if (y <= 0) {
            y = 0;
        } else if (y >= maskMax) {
            y = maskMax;
        }
        // 为遮罩设置 顶部距离 和 左侧距离
        mask.style.top = y + 'px';
        mask.style.left = x + 'px';

        /*
        大图的移动距离 / 大图的最大移动距离 = 遮挡层移动距离 / 遮挡层最大移动距离
        大图的移动距离 = 遮挡层移动距离 * 大图的最大移动距离 / 遮挡层最大移动距离
        大图的移动距离(横向) = x * bigImgW / maskMax
        大图的移动距离(纵向) = y * bigImgH / maskMax
        */
        // 获取大图对象
        var bigImg = document.querySelector('#big_box img');

        var bigImgW = bigImg.offsetWidth - big.offsetWidth;
        var bigImgH = bigImg.offsetHeight - big.offsetHeight;

        bigImg.style.top = -y * bigImgH / maskMax + 'px';
        bigImg.style.left = -x * bigImgW / maskMax + 'px';
    })



</script>

</html>